<?php
/**
 * 添加产品产品信息
 * 2013年4月19日10:09:06
 * cgp
 */
use \pft\Prod\Product;
include 'common/autoload.php';
include 'common/Prod_Common.php';
include 'common/prodField.php';
$ptype = strtoupper($_GET['type']);
$uploadUrl = 'call/uploadThumbImage.php';
$saveBtn = '<button class="ui-button ui-button-lblue" id="saveProduct" data-wrap="ticketBox" type="button">保存，下一步</button>';
$apply_did = $_SESSION['dtype']==6? $_SESSION['sid'] : $_SESSION['memberID'];
if($_SESSION['dtype']==6) {
    if(\pft\Member\MemberAccount::GetMemType($_SESSION['sid'])!=0) {
        exit('您的上级不是供应商身份，无法发布产品。');
    }
}
elseif($_SESSION['dtype']!=0 && $_SESSION['dtype']!=9) {
    exit('您不是供应商身份，无法发布产品。');
}
$obj = new Product($GLOBALS['le']);
$page_title = '产品发布';
$topicList = array();//产品主题
$delaytime = array();//延迟
if(isset($_GET['sid']) && abs($_GET['sid']>0)) {
    $page_title = '产品编辑';
    $isedit = empty($_GET['step'])? true : false ;
    $product_id = abs($_GET['sid']);

    $jdata = $obj->GetProductById($product_id);
    $apply_did = $jdata['apply_did'];
    $ptype = $jdata['p_type'];
    $topicList = explode(',', $jdata['topic']);
    $delaytime = explode('|', $jdata['delaytime']);
    //echo $ptype;
    if($jdata['attribute']) {
        //echo $jdata['attribute'];
        $package_data = $obj->PackageDataFormat($jdata['attribute']);
        $input_pack = "<input type='hidden' value='{$jdata['attribute']}' name='package_attr'/>";
    }
    $saveBtn = '<button class="ui-button ui-button-lblue" id="saveProduct" type="button">保存</button>';
}
$imgUrl = !empty($jdata['imgpath']) ? $jdata['imgpath'] :'images/defaultThum.jpg';
$package_str_tpl = '<li><p>产品名:{}</p></li>';
if($ptype == 'F' && !empty($_POST['package_data'])) {
    $attribute = urldecode($_POST['package_data']);
	// 验证包含产品的之间信息
	// exit;
	echo $attribute;
    $package_data = $obj->PackageDataFormat($attribute);
	// print_r($package_data);
	include_once("class/ProductInfo.php");//
		$ac=$GLOBALS['_ac'];
		$pw=$GLOBALS['_pw'];
		$s = new SoapClient(null,array("location" => "http://localhost/open/openService/pft_insideMX.php","uri" => "www.16u.com?ac_16u=$ac|pw_16u=$pw|auth_16u=true"));
		$proObj =  new ProductInfo($s,0,0);
		$pInfoArr = array();
		// 获取关联的产品信息
		foreach($package_data as $val){
			$pInfoArr[] = $proObj->pinfo($val['pid']);
		}
		// 验证之间的属性
		$errMsg=array();
		$payModel = $pInfoArr[0]['pay'];// 支付方式
		$advance = $pInfoArr[0]['ddays'];// 提前购买天数
		$effective = $pInfoArr[0]['ddays']+$pInfoArr[0]['delaydays'];// 有效期最小值
		foreach($pInfoArr as $k=>$val){
			if($payModel!=$val['pay']){	$errMsg[]='支付方式不一致';break;}
			
			if($val['ddays'] && ($advance<$val['ddays'])){//提前天数不一致
				$advance = $val['ddays'];
			}
			
			$ative =  $val['ddays']+$val['delaydays']; // 当前票有效期
			if($ative < $effective) $effective = $ative;
			// 判断提前天数大于有效期，那么就有一张不能用
			if($advance>$effective){
				$errMsg[]='套票产品须提前'.$advance.'天，有部分产品失效。';break;
			}

		}
		// print_r($pInfoArr);
		// print_r($package_data);
    $input_pack = "<input type='hidden' value='{$attribute}' name='package_attr'/>";
}
$page_title = "产品发布 - ";
include 'common/header.html';
include 'common/left.html';
?>
<link rel="stylesheet" href="css/aliceui.min.css"/>
<link rel="stylesheet" href="css/resetui.css"/>
<link rel="stylesheet" href="css/prod_store.css"/>
<div class="wrapper">
	<h3 class="mc_title">
		<span class="mctit_1"><span class="mctit_2">产品发布</span></span>
	</h3>
	<?if(!$isedit||isset($_GET['step'])){?>
		<ol class="ui-ministep step2">
		<li class="ui-ministep-item">
			<span class="ui-ministep-text">产品类型</span>
		</li>
		<li class="ui-ministep-item ui-ministep-active">
			<span class="ui-ministep-text"><?=$FieldList[$ptype]['ticket']['t4']?></span>
		</li>
		<li class="ui-ministep-item">
			<span class="ui-ministep-text"><?=$FieldList[$ptype]['ticket']['t1']?></span>
		</li>
	</ol>
	<?}?>
	<?
		if($ptype=='F' && !empty($package_data)) {
			
			if(count($errMsg)){
				foreach($errMsg as $k){
					echo $k,'<br/>';
				}
				exit;
			}
			include TPLPATH .'_package_box.html';
		}
	?>
	<div id="sceneryBox" class="ui-box mt10 fn-clear">
		<?if($isedit){?>
			<div class="ui-box-head">
				<h3 class="ui-box-head-title"><?=$ProdTypes[$ptype]?>信息</h3>
				<span class="ui-box-head-text"></span>
				<a href="#" class="ui-box-head-more"></a>
			</div>
		<?}?>
		<div class="ui-box-container">
			<div class="ui-box-content">
				<form name="mainForm" id="mainForm" class="ui-form" method="post" action="call/uploadThumbImage.php" enctype="multipart/form-data" target="hideFrame">
					<input type="hidden" name="lastid" value="<?=$jdata['id']?>"/>
					<input type="hidden" name="applydid" value="<?=$apply_did?>"/>
					<input type="hidden" name="thumb_img" id="thumb_img" value="<?=$imgUrl?>"/>
					<input type="hidden" name="action" value="CreateNewProduct"/>
					<input type="hidden" name="ptype" value="<?=$ptype?>"/>
					<?=$input_pack?>
					<fieldset>
						<?if(!$isedit){?>
<!--                            <input type="hidden" name="parent_id" value=""/>-->
<!--                            <div class="ui-form-item">-->
<!--                                <label class="ui-label">资源方</label>-->
<!--                                <input class="ui-input" id="saler_choice" type="text" value="">-->
<!--                                <span class="ui-form-other ui-form-explain">选择已存在的资源方</span>-->
<!--                                <ul id="salers_ul"></ul>-->
<!--                            </div>-->
						<?}?>
						<div class="ui-form-item">
							<label class="ui-label">
								<span class="ui-form-required">*</span><?=$FieldList[$ptype]['title']?>
							</label>
							<input title="请填写<?=$FieldList[$ptype]['title']?>" class="ui-input check w408" data-required="2" id="mainTitle" name="mainTitle" type="text" autofocus value="<?=$jdata['title']?>">
							<span class="ui-form-other ui-form-explain">请填写<?=$FieldList[$ptype]['title']?></span>
						</div>
						<?if(isset($FieldList[$ptype]['jtype'])){?>
							<div class="land ui-form-item">
								<?if(is_array($FieldList[$ptype]['jtype'])){?>
									<label data-s="<?=$jdata['jtype']?>" class="ui-label"><?=$FieldList[$ptype]['jtype']['t']?></label>
									<?=GetHtmlSelectByValue($FieldList[$ptype]['jtype']['list'],'jtype',$jdata['jtype'])?>
								<?} else {?>
									<label class="ui-label">
									<?=$FieldList[$ptype]['jtype']?>
									</label>
									<input class="ui-input w408" name="jtype" type="text" value="<?=$jdata['jtype']?>">
								<?}?>
								<span class="ui-form-other ui-form-explain"></span>
							</div>
						<?}?>
						<?if($f_vtime=$FieldList[$ptype]['vtime']){?>
							<div class="ui-form-item">
								<label class="ui-label"><?=$f_vtime?></label>
								<input type="radio" name="delaytime" value=1 <? if ($delaytime[0] || $delaytime[1]) echo 'checked'; ?> />是&nbsp;&nbsp;&nbsp;&nbsp;<input type="radio" name="delaytime" value=0 <? if (!$delaytime[0] && !$delaytime[1]) echo 'checked'; ?> />否&nbsp;&nbsp;&nbsp;&nbsp;
								<input type="text" name="vtimehour" class="ui-input" style="width:40px" title="请输入延迟小时" value="<?=$delaytime[0]?>"/>&nbsp;小时&nbsp;&nbsp;&nbsp;&nbsp;
								<input type="text" name="vtimeminu" class="ui-input" style="width:40px" title="请输入延迟分钟" value="<?=$delaytime[1]?>"/>&nbsp;分钟&nbsp;&nbsp;&nbsp;&nbsp;
								<br><span class="ui-form-explain">是否启用购票到验证的延迟时间，选‘是’请填入小时、分钟，格式为01小时 15分钟</span>
							</div>
						<?}?>
						<?if($FieldList[$ptype]['area']){?>
							<div class="ui-form-item">
								<label class="ui-label">
									<span class="ui-form-required">*</span>所在地区
								</label>
								<input type="hidden" value="<?=$jdata['area']?>" name="area"/>
								<span id="area"></span>
							</div>
						<?}?>
						<?if(isset($FieldList[$ptype]['address'])){?>
							<div class="land ui-form-item">
								<label class="ui-label"><span class="ui-form-required">*</span><?=$FieldList[$ptype]['address']?></label>
								<input type="text" name="mainAddress" id="address" class="w408 ui-input" title="请输入<?=$FieldList[$ptype]['address']?>" value="<?=$jdata['address']?>"/>
							</div>
						<?}?>
						<?if($FieldList[$ptype]['tel']){?>
							<div class="ui-form-item">
								<label class="ui-label">联系电话</label>
								<input type="text" name="mainPhoneNum" id="phone" class="ui-input w408" title="请输入联系电话" value="<?=$jdata['tel']?>"/>
								<span class="ui-form-explain">手机或固话（固话格式：区号-电话号码）</span>
							</div>
						<?}?>
						<?if(isset($FieldList[$ptype]['runtime'])){?>
							<div class="land ui-form-item">
								<label class="ui-label"><?=$FieldList[$ptype]['runtime']?></label>
								<input class="ui-input w408" id="runtime" name="runtime" type="text" value="<?=$jdata['runtime']?>">
								<span class="ui-form-other ui-form-explain"></span>
							</div>
						<?}?>
						<?if($FieldList[$ptype]['place']){?>
							<div class="land ui-form-item">
								<label class="ui-label"><span class="ui-form-required">*</span>出发地</label>
								<input class="ui-input s_p" data-required="1" name="start_place" type="text" value="<?=!empty($jdata['runtime'])?substr($jdata['runtime'],0,strpos($jdata['runtime'],'|')):''?>">
								<span class="ui-form-other ui-form-explain"></span>
							</div>
							<div class="land ui-form-item">
								<label class="ui-label"><span class="ui-form-required">*</span>目的地</label>
								<input class="ui-input s_p" data-required="1" name="end_place" type="text" value="<?=!empty($jdata['runtime'])?substr($jdata['runtime'],strpos($jdata['runtime'],'|')+1):''?>">
								<span class="ui-form-other ui-form-explain"></span>
							</div>
						<?}?>
						<?if($ptype!='C'){?>
						<div class="ui-form-item">
							<label class="ui-label"><?=$FieldList[$ptype]['topic']['t']?></label>
							<ul class="topicList">
								<?foreach($ProdcutTopic as $topic){?>
									<li><label><input type="checkbox" name="topic[]" <?=in_array($topic, $topicList) ? 'checked':''?> value="<?=$topic?>"/><?=$topic?></label></li>
								<?}?>
							</ul>
						</div>
						<?}?>
						<div class="ui-form-item">
							<label class="ui-label">
								<span class="ui-form-required">*</span>
								<?=$FieldList[$ptype]['jqts']['t']?>
							</label>
							<textarea id="buyTips" name="buyTips" data-required="2" title="请输入购买须知"class="ui-textarea w634"><?=isset($jdata['jqts'])?$jdata['jqts']:$FieldList[$ptype]['jqts']['tp']?></textarea>
							<a href="javascript:" title="添加一行" class="dbtn addclom">+</a>
							<a href="javascript:" title="减少一行" class="dbtn cutclom">-</a>
						</div>
						<div class="ui-form-item">
							<label class="ui-label">
								<span class="ui-form-required">*</span>
								<?=$FieldList[$ptype]['bhjq']['t']?>
							</label>
							<textarea id="detailInfo" name="detailInfo" data-required="2" title="请输入<?=$FieldList[$ptype]['bhjq']['t']?>" ><?=isset($jdata['bhjq'])?$jdata['bhjq']:$FieldList[$ptype]['bhjq']['tp']?><?=$package_content?></textarea>
						</div>
						<?if(isset($FieldList[$ptype]['jtzn'])){?>
							<div class="ui-form-item">
								<label class="ui-label"> <?=$FieldList[$ptype]['jtzn']['t']?> </label>
								<textarea id="traffic" name="trafficInfo" class="ui-textarea w634"><?=isset($jdata['jtzn'])?strip_tags(htmlspecialchars_decode($jdata['jtzn'])):$FieldList[$ptype]['jtzn']['tp']?></textarea>
								<a href="javascript:" title="添加一行" class="dbtn addclom">+</a>
								<a href="javascript:" title="减少一行" class="dbtn cutclom">-</a>
								<p class="ui-form-explain">填写交通路线等信息</p>
							</div>
						<?}?>
						<!--thumb image-->
						<div class="ui-form-item" style="position: relative;">
							<label class="ui-label">
								<span class="ui-form-required">*</span>缩略图
							</label>
							<a id="lnk_prv" target="_blank" title="点击查看原图" href="<?=$imgUrl?>">
								<img style="width:240px;height:160px;" src="<?=$imgUrl?>" id="preview" />
							</a>
							<div class="uploadImg">
								<p class="title">重新上传缩略图：</p>
								<input type="file" name="upfile" id="imgup"/>
								<input type="button" id="BtnUpload" value="上传" class="ui-button ui-button-swhite"/>
								<p class="ui-form-explain">点击上传小于1M的JPG或GIF格式的图片,<br/>建议图片尺寸：宽350像素，高230像素</p>
								<p id="waitingUpload" class="ui-form-other"></p>
							</div>
							<p class="ui-form-explain">默认将使用商户提供的缩略图作为页面展示的图片</p>
						</div>
						<!--thumb image-->
						<div class="ui-form-item"><?=$saveBtn?></div>
					</fieldset>
				</form>
			</div>
		</div>
	</div>
</div>
<ul id="mainAreas"></ul>
<iframe src="about:blank" style="display: none;" id="hideFrame" name="hideFrame" frameborder="0"></iframe>
<script src="js/jquery.getareas.js" type="text/javascript"></script>
<!-- 配置文件 -->
<script type="text/javascript" charset="utf-8" src="ue_126/ueditor_config_mini.js"></script>
<!-- 核心文件 -->
<script type="text/javascript" src="js/easydialog.min.js" ></script>
<script type="text/javascript" src="ue_126/ueditor.all.min.js"></script>
<script type="text/javascript" src="js/prod_pub.js"></script>
<script type="text/javascript" src="js/prod_editk_v3.js?body=1"></script>
<?if($ptype=='B'){?>
	<script type="text/javascript" src="js/area.js"></script>
	<script type="text/javascript" src="js/area_complete.js"></script>
<?}?>
<!--日历-->
<!--    <script type="text/javascript" src="js/Calendar.js"></script>-->
<?
include('common/footer.html')?>